import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-06-11
 * Time: 23:08
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public int ans;
    public int sumNumbers(TreeNode root) {
        int value = 0;
        dfs(value, root);
        return ans;
    }

    private void dfs(int value, TreeNode root) {
        if (root == null) {
            return;
        }
        value = value * 10 + root.val;
        if (root.left == null && root.right == null) {
            ans += value;
            return;
        }
        if (root.left != null) {
            dfs(value, root.left);
        }
        if (root.right != null) {
            dfs(value, root.right);
        }
        // 这里不用 / 10, 因为这里面是值传递, 这里的更改并不会影响上一层函数中的值
        // value /= 10
    }
}
